package com.thoughtworks.wechatmoments.logic.moments;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.thoughtworks.wechatmoments.R;
import com.thoughtworks.wechatmoments.entity.TweetsEntity;

import java.util.List;

/**
 * desc   : 评论的适配器
 * author : guowenlong
 * Email  : guowenlong20000@gmail.com
 */
public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder> {
    private List<TweetsEntity.CommentsBean> mList;

    CommentsAdapter(List<TweetsEntity.CommentsBean> comments) {
        mList = comments;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_comments, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        String content = mList.get(i).getContent();
        TweetsEntity.CommentsBean.SenderBeanX sender = mList.get(i).getSender();
        if (sender != null) {
            viewHolder.comment.setText(String.format("%s: %s", sender.getUsername(), content));
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView comment;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            comment = itemView.findViewById(R.id.tv_comment);
        }
    }
}